﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SaveSation.Common
{
    internal static class Log
    {
        public static FileStream open(string name)
        {
            string date = DateTime.Now.ToShortDateString().Replace("/", "-");
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", date);
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            FileStream _log;
            _log = File.Open(Path.Combine(path, $"{name}.log"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            _log.Position = _log.Length;
            return _log;
        }
        public static void E(string msg)
        {
            try
            {
                string str = "[" + DateTime.Now.ToString() + "][ERROR]" + msg + "\n";
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(str);
                Console.ForegroundColor = ConsoleColor.Gray;
                byte[] btArr = Encoding.UTF8.GetBytes(str);
                FileStream _log = open("error");
                _log.Write(btArr, 0, btArr.Length);
                _log.Close();
            }
            catch (Exception e1) { e1.Error(); }
        }
        public static void I(string msg)
        {
            try
            {
                string str = "[" + DateTime.Now.ToString() + "][INFO]" + msg + "\n";
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write(str);
                Console.ForegroundColor = ConsoleColor.Gray;
                byte[] btArr = Encoding.UTF8.GetBytes(str);
                FileStream _log = open("info");
                _log.Write(btArr, 0, btArr.Length);
                _log.Close();
            }
            catch (Exception e1) { e1.Error(); }
        }
        public static void IW(string msg)
        {
            try
            {
                string str = "[" + DateTime.Now.ToString() + "][WRAN]" + msg + "\n";
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write(str);
                Console.ForegroundColor = ConsoleColor.Gray;
                byte[] btArr = Encoding.UTF8.GetBytes(str);
                FileStream _log = open("info");
                _log.Write(btArr, 0, btArr.Length);
                _log.Close();
            }
            catch (Exception e1) { e1.Error(); }
        }
    }
}
